package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSON;
import com.turbo.boot.crawler.dao.dal.CrawlerResultDal;
import com.turbo.boot.crawler.dao.entity.CrawlerResultEntity;
import com.turbo.boot.crawler.spider.worker.BaseTurboApiCrawlerPageParser;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/4/19
 */
@Service("balticExchangeCrawlerPageParser")
public class BalticExchangeCrawlerPageParser extends BaseTurboApiCrawlerPageParser<BalticExchangeCrawlerPageParser.PageVo> {


    @Resource
    private CrawlerResultDal crawlerResultDal;

    @Override
    public void parse(PageVo pageVo) {
        // 解析封装 PageVo 对象
        if (StringUtils.isEmpty(pageVo.getIndexName())) {
            return;
        }
        
        // 处理业务代码
        CrawlerResultEntity record = buildCrawlerResultEntity();
        record.setCollectedAt(new Date());
        // 波罗的海
        record.setQuotaCode(pageVo.getIndexDataSetName());
        record.setQuotaName(pageVo.getIndexDataSetName());
        record.setQuotaValue(pageVo.getCurrent().getValue());

        crawlerResultDal.insert(record);
        System.out.println(pageVo.toString());
    }


    @Setter
    @Getter
    public static class PageVo {

        private String indexId;
        private String indexName;
        private String indexDataSetName;
        private Previous previous;
        private Current current;

        @Setter
        @Getter
        public static class Current {
            private String value;
            private Date indexDate;
        }

        @Setter
        @Getter
        public static class Previous {
            private String value;
            private Date indexDate;
        }

        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }
}
